package com.example.hospitallogin.modules.login.bff;
import com.example.hospitallogin.assembly.EmailConfig;
import com.example.hospitallogin.assembly.PageData;
import com.example.hospitallogin.modules.login.dao.entity.EmailEntity;
import com.example.hospitallogin.modules.login.dao.entity.HospitalMessageEntity;
import com.example.hospitallogin.modules.login.dto.ApplyListReq;
import com.example.hospitallogin.modules.login.dto.ApprovalApplyReq;
import com.example.hospitallogin.modules.login.dto.HospitalRegisterRsp;
import com.example.hospitallogin.modules.login.service.HospitalManegerService;
import com.example.hospitallogin.modules.register.dao.entity.HospitalRegisterEntity;
import com.example.hospitallogin.modules.register.service.RegisterHospitalService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qiancheng@cqxyy.net
 * @date 2022/9/19
 */
@Service
public class HospitalRegisterImpl {

    @Autowired
    private RegisterHospitalService registerHospitalService;

    @Autowired
    private HospitalManegerService hospitalManegerService;
    @Autowired
    private EmailConfig emailConfig;
    /**
     * 同意医院申请
     * @param req
     * @return
     */
    @Transactional
    public Object approvalApply(ApprovalApplyReq req) {
        Example example = new Example(HospitalRegisterEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uuid",req.getUuid());
        List<HospitalRegisterEntity> list = registerHospitalService.selectByExample(example);
        HospitalMessageEntity hospitalMessageEntity = new HospitalMessageEntity();
        if(CollectionUtils.isEmpty(list)){
            return "此申请不存在";
        }
        BeanUtils.copyProperties(list.get(0),hospitalMessageEntity);
        //发送邮箱
        EmailEntity emailEntity = new EmailEntity();
        emailEntity.setEmailAddress(hospitalMessageEntity.getEmil());
        emailEntity.setHospitalName(hospitalMessageEntity.getHospitalName());
        emailEntity.setName(hospitalMessageEntity.getManager());
        emailEntity.setStatus(1);
        emailConfig.sendEmails(emailEntity);
        registerHospitalService.deleteByExample(example);
        hospitalManegerService.insertSelective(hospitalMessageEntity);
        return "";
    }

    /**
     * 拒绝医院申请
     * @param req
     * @return
     */
    @Transactional
    public Object refuseApply(ApprovalApplyReq req){
        Example example = new Example(HospitalRegisterEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uuid",req.getUuid());
        List<HospitalRegisterEntity> list = registerHospitalService.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return "此申请不存在";
        }
        HospitalRegisterEntity hospitalRegisterEntity = list.get(0);
        EmailEntity emailEntity = new EmailEntity();
        emailEntity.setContext(req.getResult());
        emailEntity.setStatus(2);
        emailEntity.setEmailAddress(hospitalRegisterEntity.getEmil());
        emailEntity.setName(hospitalRegisterEntity.getManager());
        emailEntity.setHospitalName(hospitalRegisterEntity.getHospitalName());
        emailConfig.sendEmails(emailEntity);
        registerHospitalService.deleteByExample(example);
        return "";
    }

    /**
     * 查看医院申请列表
     * @param req
     * @return
     */
    public PageData<HospitalRegisterRsp> applyList(ApplyListReq req) {
        com.github.pagehelper.Page<HospitalRegisterRsp> page = PageHelper.startPage(
                req.getCurrentPage(), req.getPageSize()
        );
        Example example = new Example(HospitalRegisterEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(req.getHospitalName())){
            criteria.andLike("hospitalName","%" + req.getHospitalName() + "%");
        }
        if(StringUtils.isNotBlank(req.getPhone())){
            criteria.andEqualTo("phone",req.getPhone());
        }
        List<HospitalRegisterRsp> list = registerHospitalService.selectByExample(example).stream().map(s ->{
            HospitalRegisterRsp hospitalRegisterRsp = new HospitalRegisterRsp();
            BeanUtils.copyProperties(s,hospitalRegisterRsp);
            hospitalRegisterRsp.setHospitalUuid(s.getUuid());
            return hospitalRegisterRsp;
        }).collect(Collectors.toList());

          int longs = ((Number) page.getTotal()).intValue();
          PageData<HospitalRegisterRsp> pageData = PageData.of(list,page.getPageNum(),page.getPageSize(),longs);
          return pageData;
//        return PageTransUtil.createPageList(page,HospitalRegisterRsp.class);
    }
}
